package com.shihb.creationalpatterns;

/**
 *Description:创建者模式，创建对象的最佳方式
 * 负责对象的构建和它的表示分离
 *Version:1.0.0
 *@author shihb
 *@date 2020/4/21 8:21
 */
public class BuilderPattern {

  public static void main(String[] args) {

    AbstractBuilder builder=new CommonBulid();
    Direct direct = new Direct(builder);
    System.out.println(direct.construction());
    direct.setBuild(new HighBulid());
    System.out.println(direct.construction());

//    BaseBuilder baseBuilder= new waiterBuilder();
//    System.out.println(baseBuilder.getProduct());
//    baseBuilder.buildA("雪鱼汉堡").buildD("吮指原味鸡块");
//    System.out.println(baseBuilder.getProduct());
  }

}

/**
 * 构建者的抽象对象 制作套餐的抽象类
 */
abstract class BaseBuilder{
  abstract BaseBuilder buildA(String s);
  abstract BaseBuilder buildB(String s);
  abstract BaseBuilder buildC(String s);
  abstract BaseBuilder buildD(String s);
  abstract Product getProduct();
}

/**
 * 产品，也就是我们要用构建的那个复杂对象 套餐
 */
class Product {
  private String A="汉堡";
  private String B="可乐";
  private String C="薯条";
  private String D="鸡翅";

  @Override
  public String toString() {
    return "Product{" +
        "A='" + A + '\'' +
        ", B='" + B + '\'' +
        ", C='" + C + '\'' +
        ", D='" + D + '\'' +
        '}';
  }

  public void setA(String a) {
    A = a;
  }

  public void setB(String b) {
    B = b;
  }

  public void setC(String c) {
    C = c;
  }

  public void setD(String d) {
    D = d;
  }
}

/**
 * 构建者 具体构建对象的类。 服务员
 */
class waiterBuilder extends BaseBuilder{

  private Product product;

  public waiterBuilder() {
    // 有个默认产品
    this.product=new Product();
  }

  @Override
  BaseBuilder buildA(String a) {
    this.product.setA(a);
    return this;
  }

  @Override
  BaseBuilder buildB(String b) {
    this.product.setB(b);
    return this;
  }

  @Override
  BaseBuilder buildC(String c) {
    this.product.setC(c);
    return this;
  }

  @Override
  BaseBuilder buildD(String d) {
    this.product.setD(d);
    return this;
  }

  @Override
  Product getProduct() {
    return this.product;
  }
}




/**
 * 建房子，图纸
 */
abstract class AbstractBuilder {
  protected House house =new House();
  abstract void buildBase(); // 地基
  abstract void buildWall(); // 墙体
  abstract void buildRoof(); // 屋顶
  House buildHouse(){
    return  this.house;
  }; //建房子

}
/**
 * 要建造的产品
 */
class House  {
  private String base;// 地基
  private String wall;// 墙
  private String roof;// 屋顶

  public String getBase() {
    return base;
  }

  public void setBase(String base) {
    this.base = base;
  }

  @Override
  public String toString() {
    return "House{" +
        "base='" + base + '\'' +
        ", wall='" + wall + '\'' +
        ", roof='" + roof + '\'' +
        '}';
  }

  public String getWall() {
    return wall;
  }

  public void setWall(String wall) {
    this.wall = wall;
  }

  public String getRoof() {
    return roof;
  }

  public void setRoof(String roof) {
    this.roof = roof;
  }
}

/**
 *工人 具体建房子的人
 */
class CommonBulid extends AbstractBuilder{


  @Override
  void buildBase() {
    super.house.setBase("普通房子的地基");
  }

  @Override
  void buildWall() {
    super.house.setWall("普通房子的墙");
  }

  @Override
  void buildRoof() {
    super.house.setRoof("普通房子的屋顶");
  }
}
class HighBulid extends AbstractBuilder{


  @Override
  void buildBase() {
    super.house.setBase("高端房子的地基");
  }

  @Override
  void buildWall() {
    super.house.setWall("高端房子的墙");
  }

  @Override
  void buildRoof() {
    super.house.setRoof("高端房子的屋顶");
  }
}
/**
 *指挥 负责指挥工人建造房子
 */
class Direct {

  private AbstractBuilder build;

  public Direct(AbstractBuilder build) {
    this.build = build;
  }

  public void setBuild(AbstractBuilder build) {
    this.build = build;
  }

  public House construction(){
    this.build.buildBase();
    this.build.buildWall();
    this.build.buildRoof();
    return this.build.buildHouse();

  }
}
